/*
 * @lc app=leetcode.cn id=1656 lang=typescript
 *
 * [1656] 设计有序流
 */

// @lc code=start
class OrderedStream {
    private inner: (undefined | string)[] = [];
    ptr = 1;
    constructor(n: number) {
        this.inner = new Array(n).fill(undefined);
    }

    private refresh() {
        let cache = [];
        let i = this.ptr - 1;
        while (this.inner[i]) {
            cache.push(this.inner[i]!);
            i++;
        }

        this.ptr = i + 1;
        return cache;
    }
    insert(idKey: number, value: string): string[] {
        this.inner[idKey - 1] = value;

        return this.refresh();
    }
}

/**
 * Your OrderedStream object will be instantiated and called as such:
 * var obj = new OrderedStream(n)
 * var param_1 = obj.insert(idKey,value)
 */
// @lc code=end
